export function formatDate(dtime, fmt) {
    let date = new Date(dtime)
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));

    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

export function getAge(dtime) {
    let today = new Date();
    let birthDate = new Date(dtime);
    let age = today.getFullYear() - birthDate.getFullYear();
    let m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
}

export function timeDiff(dtime) {
    let today = new Date()
    let birthDate = new Date(dtime)
    let daysTotal = 0
    let year = today.getFullYear(); //当前年份  
    let _month = today.getMonth() + 1; //当前月份  
    let _day = today.getDate(); //当前天数  
    let month = birthDate.getMonth() + 1; //生日月份  
    let day = birthDate.getDate() // 生日天数
    if (month > _month) {
        for (let j = _month; j < month; j++) {
            daysTotal = TotalDays(year, j, daysTotal);
        }
        daysTotal = daysTotal + day - _day;
    } else if (month < _month) {
        for (let k = month; k < _month; k++) {
            daysTotal = TotalDays(year, k, daysTotal);
        }
        let dayFull = TotalDays(year, 0, daysTotal);
        daysTotal = dayFull - daysTotal + day - _day;
    } else {
        if (day >= _day) {
            daysTotal = day - _day;
        } else {
            daysTotal = TotalDays(year, 0, daysTotal) - (_day - day);
        }
    }
    return daysTotal;
}

//当前时间格式化
export function getCurrentTime() {
    let dtCur = new Date();
    let yearCur = dtCur.getFullYear();
    let monCur = dtCur.getMonth() + 1;
    let dayCur = dtCur.getDate();
    let hCur = dtCur.getHours();
    let mCur = dtCur.getMinutes();
    // let sCur = dtCur.getSeconds();
    let timeCur = yearCur + "-" + (monCur < 10 ? "0" + monCur : monCur) + "-" +
        (dayCur < 10 ? "0" + dayCur : dayCur) + " " + (hCur < 10 ? "0" + hCur : hCur) +
        ":" + (mCur < 10 ? "0" + mCur : mCur)
        //alert(timeCur);// 输出时间  
    return timeCur;
}

export function getAddressList(addressData) {

}

//js 判断闰年
function isLeapYear(Year) {
    if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
        return (true);
    } else { return (false); }
}


function TotalDays(year, month, days) {
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            days += 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            days += 30;
            break;
        case 2:
            if (isLeapYear(year)) {
                days += 29;
            } else {
                days += 28;
            }
            break;
        default:
            if (isLeapYear(year)) {
                days = 366;
            } else {
                days = 365;
            }
            break;
    }
    return days;
}
export function getHiddenText(mobile) {
    if (!mobile) {
        return ''
    }
    let regExp = /(\d{3})\d+(\d{4})/
    let mobileTxt = mobile.replace(regExp, "$1****$2")
    return mobileTxt
}